<template>
    <div class="login_container">
        <div class="login_box">
            <div class="login_right">
                <div class="login_form">
                    <el-form
                        :model="loginForm"
                        :rules="loginFormRules"
                        ref="loginFormRef"
                    >
                        <div class="restive">
                            <h2>忘记密码</h2>
                            <a href="#/login">返回登录页</a>
                        </div>
                        <p>账号</p>
                        <el-form-item prop="username">
                            <el-input
                                prefix-icon="iconfont icon-user"
                                v-model="loginForm.username"
                                placeholder="请输入用户名"
                            ></el-input>
                        </el-form-item>
                        <p>手机号</p>
                        <el-form-item prop="phone">
                            <el-input
                                v-model="loginForm.phone"
                                clearable
                                placeholder="请输入手机号"
                            ></el-input>
                        </el-form-item>
                        <p>验证码</p>
                        <el-form-item prop="code">
                            <el-input
                                v-model="loginForm.code"
                                clearable
                                placeholder="请输入验证码"
                            >
                                <el-button
                                    slot="suffix"
                                    style="
                                        color: blue;
                                        border-radius: 5px;
                                        height: 100%;
                                        background-color: #fff;
                                    "
                                    :disabled="disabled"
                                    @click="getCode"
                                >
                                    {{ btnTxt }}
                                </el-button>
                            </el-input>
                        </el-form-item>
                        <p>新密码</p>
                        <el-form-item prop="password">
                            <el-input
                                prefix-icon="iconfont icon-3702mima"
                                v-model="loginForm.pwd"
                                show-password
                                type="password"
                                placeholder="请输入新密码"
                                auto-complete="new-password"
                            ></el-input>
                        </el-form-item>

                        <div class="btn">
                            <el-button type="primary" @click="modify"
                                >提交</el-button
                            >
                        </div>
                    </el-form>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { modifyUserApi } from "@/api/loginApi.js"
export default {
    name: "register",
    data() {
        let validatePhone = (rule, value, callback) => {
            if (!value) {
                callback(new Error("手机号不能为空"))
            }
            if (!/^1[3456789]\d{9}$/.test(value)) {
                callback(new Error("手机号不正确"))
            }
            callback()
        }

        return {
            btnTxt: "获取验证码",
            disabled: false,
            time: 0,
            str: "",
            regForm: {}, // 注册表单对象
            code: "", // 验证码
            loginForm: {
                username: "",
                pwd: "",
                phone: "",
                code: "",
            },
            loginFormRules: {
                username: [
                    {
                        required: true,
                        message: "请输入用户名",
                        trigger: "blur",
                    },
                    {
                        min: 3,
                        max: 10,
                        message: "用户名长度在3到10个字符",
                        trigger: "blur",
                    },
                ],
                pwd: [
                    { required: true, message: "请输入密码", trigger: "blur" },
                    {
                        min: 6,
                        max: 15,
                        message: "密码长度在6到15个字符",
                        trigger: "blur",
                    },
                ],
                phone: [
                    {
                        required: true,
                        validator: validatePhone,
                        trigger: "blur",
                    },
                ],
                code: [
                    {
                        required: true,
                        message: "请输入验证码",
                        trigger: "blur",
                    },
                ],
            },
        }
    },
    methods: {
        resLoginForm() {
            this.$refs.loginFormRef.resetFields()
        },
        getCode() {
            if (!this.loginFormRules.phone) {
                this.$message.warning("手机号格式有误")
            } else if (!this.loginForm.phone) {
                this.$message.warning("请先输入手机号码")
            } else if (!/^1[3456789]\d{9}$/.test(this.loginForm.phone)) {
                this.$message.warning("手机号格式有误")
            } else {
                this.time = 30
                this.disabled = true
                this.timer()
                this.$message.success(`当前手机验证码为  ${this.getCodes()}`)
            }
        },
        timer() {
            if (this.time > 0) {
                this.time--
                this.btnTxt = this.time + "s后重新获取验证码"
                setTimeout(this.timer, 1000)
            } else {
                this.time = 0
                this.btnTxt = "获取验证码"
                this.disabled = false
            }
        },
        getCodes() {
            this.str = ""
            var items = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
            for (var i = 0; i < 6; i++) {
                var n = Math.floor(Math.random() * items.length)
                this.str += items[n]
            }
            return this.str
        },
        modify() {
            this.$refs.loginFormRef.validate((valid) => {
                if (valid) {
                    if (this.loginForm.code == this.str) {
                        modifyUserApi(this.loginForm).then((res) => {
                            if (res.code == 200) {
                                this.$message.success(res.msg)
                                this.$router.push("/login")
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    } else {
                        this.$message.error("验证码输入错误")
                    }
                }
            })
        },
    },
}
</script>

<style lang="less" scoped>
.login_container {
    background: url(../../assets/images/Denglu/beijing.png);
    background-size: 100% 100%;
    height: 100%;
}
.login_box {
    width: 650px;
    height: 380px;
    background-color: #fff;
    border-radius: 20px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: url(../../assets/images/Denglu/denglu_03.png);
    background-size: 100% 100%;
}
.login_right {
    width: 50%;
    height: 100%;
    float: right;
    .login_form {
        box-sizing: border-box;
        width: 100%;
        height: 100%;
        padding: 0 40px;
        /deep/.el-input__suffix {
            right: 0px;
        }
        .restive {
            width: 100%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 35px 0 25px 0;
        }
        h2 {
            font-size: 20px;
        }
        p {
            font-size: 12px;
            padding: 5px 0;
        }
        .el-form-item {
            margin-bottom: 10px;
        }
        .login_a {
            text-align: right;
            padding-bottom: 10px;
        }
        a {
            text-decoration: none;
            font-size: 12px;
            cursor: pointer;
        }
    }
}
.btn {
    margin-top: 15px;
    width: 100%;
    height: 35px;
    .el-button {
        width: 100%;
        height: 100%;
        color: #fff;
        font-size: 12px;
    }
}
/deep/.el-input__inner {
    padding-left: 10px !important;
    height: 30px;
}
</style>
